package book

class BookService {
	boolean transactional = true
	
	def getStaticBook() {
		return new book.Book(
			  author: "Anynomous",
			  category: BookCat.Science,
			  description:"This is a test book",
			  isbn: "ISBN 98-7654-321-0",
			  language: "english",
			  maxGrade: 2,
			  minGrade: 1,
			  title: "NoTitle"
			)
	}
	
	def getRandomBook()
	{
		def allBooks = Book.list()
		def randomBook = null
		
		if(allBooks.size() > 0)
		{
			def randomIndex = new Random().nextInt(allBooks.size())
			randomBook = allBooks[randomIndex]
		}
		else
		{
			randomBook = getStaticBook()
		}
		
		return randomBook
	}
}
